from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# HOST = os.environ['DB_HOST']
HOST = '47.104.31.138'
# PORT = os.environ['DB_PORT']
PORT = 3306
# USER = os.environ['DB_USER']
USER = 'carsys2004_admin'
# PASS = os.environ['DB_PASS']
PASS = 'Carsys.2004'

DATABASE_URL = f'mysql://{USER}:{PASS}@{HOST}:{PORT}/carsys2004?charset=utf8'

engine = create_engine(DATABASE_URL, echo=True)
db_session_factory = sessionmaker(autocommit=False, autoflush=False, bind=engine)


def get_db_session():
    session = db_session_factory()
    try:
        yield session
        session.commit()
    except SQLAlchemyError as err:
        print(err)
        session.rollback()
    finally:
        session.close()


Base = declarative_base()
